package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.TUser;
import com.zr.service.RoleService;
import com.zr.service.UserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@Scope(value="prototype")
public class UserAction extends  ActionSupport  implements  ServletResponseAware{
	
	private  TUser user;
	private  int page;
	private  int rows;
	private  HttpServletResponse  resp;
	private String jsonadd;
	private int[] uids;
	private String jsont;
	
	
    public String getJsont() {
		return jsont;
	}
	public void setJsont(String jsont) {
		this.jsont = jsont;
	}
	public int[] getUids() {
		return uids;
	}
	public void setUids(int[] uids) {
		this.uids = uids;
	}
	public String getJsonadd() {
		return jsonadd;
	}
	public void setJsonadd(String jsonadd) {
		this.jsonadd = jsonadd;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public TUser getUser() {
		return user;
	}
	public void setUser(TUser user) {
		this.user = user;
	}
	@Resource
	private  UserService  userService;
	@Resource
	private RoleService roleService;
	public String login(){
	  TUser currentuser =userService.findUserByuserNameanduserPsw(this.getUser());
	  if(null!=currentuser){
		  return SUCCESS;
	  }
		return ERROR;
	}
	
	public void showUserMsg(){
		JSONObject  obj = userService.findUserMsg(this.getPage(), this.getRows());
		try {
			resp.setCharacterEncoding("utf8");
			resp.getWriter().write(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	 
	public void getRname() {
		JSONArray jsa=roleService.getRole();
		try {
			resp.setCharacterEncoding("utf8");
			PrintWriter pw=resp.getWriter();
			pw.write(jsa.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void deleteUser() throws IOException {
		int uids[]=this.getUids();
		for (int i = 0; i < uids.length; i++) {
		userService.deleteUser(uids[i]);
		}
		resp.setCharacterEncoding("utf8");
		PrintWriter pw=resp.getWriter();
		pw.write("删除成功");
			
	}
	public void addUser() {
		JSONObject jso=JSONObject.fromObject(this.getJsonadd());
		int rid=jso.getInt("rid");
		String uacount=jso.getString("uacount");
		String upassword=jso.getString("upassword");
		String uname=jso.getString("uname");
		Boolean flag=userService.addUser(uacount, upassword, uname, rid);
		try {
			PrintWriter pw=resp.getWriter();
			pw.write(flag+"");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void updateUser() {
		JSONObject jso=JSONObject.fromObject(this.getJsont());
		int uid=jso.getInt("uid");
		int rid=jso.getInt("rid");
		String uacount=jso.getString("uacount");
		String upassword=jso.getString("upassword");
		String uname=jso.getString("uname");
		userService.updateUser(uid, uacount, upassword, uname, rid);
		System.out.println(111);
		try {
			PrintWriter pw=resp.getWriter();
			pw.write("修改成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.resp = response;
		
	}
}
